package io.renren.modules.job.controller;

import com.github.pagehelper.PageInfo;
import io.renren.common.annotation.SysLog;
import io.renren.common.pojo.R;
import io.renren.common.validator.ValidatorUtils;
import io.renren.modules.common.BaseController;
import io.renren.modules.job.entity.ScheduleJobEntity;
import io.renren.modules.job.pojo.ScheduleJobQuery;
import io.renren.modules.job.service.ScheduleJobService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * 定时任务
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2016年11月28日 下午2:16:40
 */
@RestController
@RequestMapping("/sys/schedule")
@Api(description = "定时任务接口")
public class ScheduleJobController extends BaseController{
	@Autowired
	private ScheduleJobService scheduleJobService;
	
	/**
	 * 定时任务列表
	 */
	@RequestMapping(value = "/list",method = RequestMethod.GET)
	@PreAuthorize("hasAuthority('sys:schedule:list')")
	@ApiOperation("获取定时任务列表")
	public R<PageInfo<ScheduleJobEntity>> list(ScheduleJobQuery query){
		PageInfo<ScheduleJobEntity> pageInfo = scheduleJobService.queryPage(query);
		return R.ok(pageInfo);
	}
	
	/**
	 * 定时任务信息
	 */
	@RequestMapping(value = "/info/{jobId}",method = RequestMethod.GET)
	@PreAuthorize("hasAuthority('sys:schedule:info')")
	@ApiOperation("获取的定时任务信息")
	public R<ScheduleJobEntity> info(@ApiParam("定时任务ID") @PathVariable("jobId") Long jobId){
		ScheduleJobEntity schedule = scheduleJobService.queryObject(jobId);
		return R.ok(schedule);
	}
	
	/**
	 * 保存定时任务
	 */
	@SysLog("保存定时任务")
	@RequestMapping(value = "/",method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('sys:schedule:save')")
	@ApiOperation("保存定时任务")
	public R save(@ApiParam("定时任务对象") @RequestBody ScheduleJobEntity scheduleJob){
		ValidatorUtils.validateEntity(scheduleJob);
		
		scheduleJobService.save(scheduleJob);
		return R.ok();
	}
	
	/**
	 * 修改定时任务
	 */
	@SysLog("修改定时任务")
	@RequestMapping(value = "/",method = RequestMethod.PUT)
	@PreAuthorize("hasAuthority('sys:schedule:update')")
	@ApiOperation("修改定时任务")
	public R update(@ApiParam("定时任务对象") @RequestBody ScheduleJobEntity scheduleJob){
		ValidatorUtils.validateEntity(scheduleJob);
				
		scheduleJobService.update(scheduleJob);
		return R.ok();
	}
	
	/**
	 * 删除定时任务
	 */
	@SysLog("删除定时任务")
	@RequestMapping(value = "/",method = RequestMethod.DELETE)
	@PreAuthorize("hasAuthority('sys:schedule:delete')")
	@ApiOperation("删除定时任务")
	public R delete(@ApiParam("定时任务ID列表") @RequestBody Long[] jobIds){
		scheduleJobService.deleteBatch(jobIds);
		return R.ok();
	}
	
	/**
	 * 立即执行任务
	 */
	@SysLog("立即执行任务")
	@RequestMapping(value = "/run",method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('sys:schedule:run')")
	@ApiOperation("立即执行任务")
	public R run(@ApiParam("定时任务ID列表") @RequestBody Long[] jobIds){
		scheduleJobService.run(jobIds);
		return R.ok();
	}
	
	/**
	 * 暂停定时任务
	 */
	@SysLog("暂停定时任务")
	@RequestMapping(value = "/pause",method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('sys:schedule:pause')")
	@ApiOperation("暂停定时任务")
	public R pause(@ApiParam("定时任务ID列表") @RequestBody Long[] jobIds){
		scheduleJobService.pause(jobIds);
		return R.ok();
	}
	
	/**
	 * 恢复定时任务
	 */
	@SysLog("恢复定时任务")
	@RequestMapping(value = "/resume",method = RequestMethod.POST)
	@PreAuthorize("hasAuthority('sys:schedule:resume')")
	@ApiOperation("恢复定时任务")
	public R resume(@ApiParam("定时任务ID列表") @RequestBody Long[] jobIds){
		scheduleJobService.resume(jobIds);
		return R.ok();
	}

}
